#ifndef THINK_THINK_CONSOLE_COMMAND_OPTIMIZE_SCHEMA_H
#define THINK_THINK_CONSOLE_COMMAND_OPTIMIZE_SCHEMA_H

static char* think_console_command_optimize_schema =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: yunwuxin <448901948@qq.com>\n"
	"// +----------------------------------------------------------------------\n"
	"namespace think\\console\\command\\optimize;\n"
	"\n"
	"use think\\App;\n"
	"use think\\console\\Command;\n"
	"use think\\console\\Input;\n"
	"use think\\console\\input\\Option;\n"
	"use think\\console\\Output;\n"
	"use think\\Db;\n"
	"\n"
	"class Schema extends Command\n"
	"{\n"
	"    /** @var  Output */\n"
	"    protected $output;\n"
	"\n"
	"    protected function configure()\n"
	"    {\n"
	"        $this->setName(\'optimize:schema\')\n"
	"            ->addOption(\'config\', null, Option::VALUE_REQUIRED, \'db config .\')\n"
	"            ->addOption(\'db\', null, Option::VALUE_REQUIRED, \'db name .\')\n"
	"            ->addOption(\'table\', null, Option::VALUE_REQUIRED, \'table name .\')\n"
	"            ->addOption(\'module\', null, Option::VALUE_REQUIRED, \'module name .\')\n"
	"            ->setDescription(\'Build database schema cache.\');\n"
	"    }\n"
	"\n"
	"    protected function execute(Input $input, Output $output)\n"
	"    {\n"
	"        if (!is_dir(RUNTIME_PATH . \'schema\')) {\n"
	"            @mkdir(RUNTIME_PATH . \'schema\', 0755, true);\n"
	"        }\n"
	"        $config = [];\n"
	"        if ($input->hasOption(\'config\')) {\n"
	"            $config = $input->getOption(\'config\');\n"
	"        }\n"
	"        if ($input->hasOption(\'module\')) {\n"
	"            $module = $input->getOption(\'module\');\n"
	"            // 读取模型\n"
	"            $path = APP_PATH . $module . DS . \'model\';\n"
	"            $list = is_dir($path) ? scandir($path) : [];\n"
	"            $app  = App::$namespace;\n"
	"            foreach ($list as $file) {\n"
	"                if (0 === strpos($file, \'.\')) {\n"
	"                    continue;\n"
	"                }\n"
	"                $class = \'\\\\\' . $app . \'\\\\\' . $module . \'\\\\model\\\\\' . pathinfo($file, PATHINFO_FILENAME);\n"
	"                $this->buildModelSchema($class);\n"
	"            }\n"
	"            $output->writeln(\'<info>Succeed!</info>\');\n"
	"            return;\n"
	"        } elseif ($input->hasOption(\'table\')) {\n"
	"            $table = $input->getOption(\'table\');\n"
	"            if (!strpos($table, \'.\')) {\n"
	"                $dbName = Db::connect($config)->getConfig(\'database\');\n"
	"            }\n"
	"            $tables[] = $table;\n"
	"        } elseif ($input->hasOption(\'db\')) {\n"
	"            $dbName = $input->getOption(\'db\');\n"
	"            $tables = Db::connect($config)->getTables($dbName);\n"
	"        } elseif (!\\think\\Config::get(\'app_multi_module\')) {\n"
	"            $app  = App::$namespace;\n"
	"            $path = APP_PATH . \'model\';\n"
	"            $list = is_dir($path) ? scandir($path) : [];\n"
	"            foreach ($list as $file) {\n"
	"                if (0 === strpos($file, \'.\')) {\n"
	"                    continue;\n"
	"                }\n"
	"                $class = \'\\\\\' . $app . \'\\\\model\\\\\' . pathinfo($file, PATHINFO_FILENAME);\n"
	"                $this->buildModelSchema($class);\n"
	"            }\n"
	"            $output->writeln(\'<info>Succeed!</info>\');\n"
	"            return;\n"
	"        } else {\n"
	"            $tables = Db::connect($config)->getTables();\n"
	"        }\n"
	"\n"
	"        $db = isset($dbName) ? $dbName . \'.\' : \'\';\n"
	"        $this->buildDataBaseSchema($tables, $db, $config);\n"
	"\n"
	"        $output->writeln(\'<info>Succeed!</info>\');\n"
	"    }\n"
	"\n"
	"    protected function buildModelSchema($class)\n"
	"    {\n"
	"        $reflect = new \\ReflectionClass($class);\n"
	"        if (!$reflect->isAbstract() && $reflect->isSubclassOf(\'\\think\\Model\')) {\n"
	"            $table   = $class::getTable();\n"
	"            $dbName  = $class::getConfig(\'database\');\n"
	"            $content = \'<?php \' . PHP_EOL . \'return \';\n"
	"            $info    = $class::getConnection()->getFields($table);\n"
	"            $content .= var_export($info, true) . \';\';\n"
	"            file_put_contents(RUNTIME_PATH . \'schema\' . DS . $dbName . \'.\' . $table . EXT, $content);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    protected function buildDataBaseSchema($tables, $db, $config)\n"
	"    {\n"
	"        if (\'\' == $db) {\n"
	"            $dbName = Db::connect($config)->getConfig(\'database\') . \'.\';\n"
	"        } else {\n"
	"            $dbName = $db;\n"
	"        }\n"
	"        foreach ($tables as $table) {\n"
	"            $content = \'<?php \' . PHP_EOL . \'return \';\n"
	"            $info    = Db::connect($config)->getFields($db . $table);\n"
	"            $content .= var_export($info, true) . \';\';\n"
	"            file_put_contents(RUNTIME_PATH . \'schema\' . DS . $dbName . $table . EXT, $content);\n"
	"        }\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif